package com.nuc.workflowserver.controller;

import com.nuc.workflowserver.common.model.ResultModel;
import com.nuc.workflowserver.model.dto.CheckWorkDTO;
import com.nuc.workflowserver.model.dto.PageDTO;
import com.nuc.workflowserver.model.pojo.User;
import com.nuc.workflowserver.model.pojo.UserInfo;
import com.nuc.workflowserver.model.pojo.Work;
import com.nuc.workflowserver.service.IUserInfoService;
import com.nuc.workflowserver.service.IWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("work")
@Api(value = "工作量管理接口", description = "提供工作量管理维护")
public class WorkController {

    @Autowired
    IWorkService iWorkService;

    @Autowired
    IUserInfoService iUserInfoService;

    @PostMapping("/insertWork")
    @ApiOperation("工作上报")
    public ResultModel deleteUserInfo(@RequestBody Work work,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user && null != user.getId()) {
            work.setUserId(user.getId());
        } else {
            return new ResultModel("请先登录系统！", false);
        }
        Integer res = iWorkService.insert(work);
        if (res > 0) {
            return new ResultModel("上报成功！", true);
        } else {
            return new ResultModel("上报失败，请联系管理员！", false);
        }
    }

    @PostMapping("/selectWorkPage")
    @ApiOperation("工作上报")
    public ResultModel selectWorkPage(@RequestBody PageDTO pageDTO,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        Work work = new Work();
        if (null != user && null != user.getId()) {
            work.setUserId(user.getId());
        } else {
            return new ResultModel("请先登录系统！", false);
        }
        Object o = iWorkService
                .selectPage(work, pageDTO.getPageNum(), pageDTO.getPageSize());
        return new ResultModel(o, true);
    }

    @PostMapping("/selectCheckWorkPage")
    @ApiOperation("工作上报审核")
    public ResultModel selectCheckWork(@RequestBody CheckWorkDTO checkWorkDTO,
            HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user && null != user.getId()) {
            UserInfo userInfo = iUserInfoService
                    .selectByPrimary(new UserInfo(user.getId()));
            Object o = iWorkService
                    .selectCheckWork(userInfo.getFlowId(), checkWorkDTO);
            return new ResultModel(o, true);
        } else {
            return new ResultModel("请先登录系统！", false);
        }
    }

    @PostMapping("/update")
    @ApiOperation("审核")
    public ResultModel update(@RequestBody Work work) {
        Integer res = iWorkService.update(work);
        if (res > 0) {
            return new ResultModel("审核完成", true);
        } else {
            return new ResultModel("审核失败，请联系管理员！", true);
        }
    }

    @PostMapping("/selectWorkForEcharts")
    @ApiOperation("员工时间横向对比")
    public ResultModel selectWorkForEcharts(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (null != user && null != user.getId()) {
            List<Work> list = iWorkService.selectWorkForEcharts(user.getId());
            return new ResultModel(list, true);
        } else {
            return new ResultModel("请先登录系统！", false);
        }
    }
}
